package com.gitee.tje.waving.factory.config;

/**
 * 默认属性处理器
 *
 * @author taojiaen
 * @date 2018-02-09 21:06
 */
public class DefaultPropertyEditor implements PropertiesEditor{
    @Override
    public boolean canEdit(Class<?> clazz) {
        return true;
    }

    @Override
    public Object edit(Class<?> clazz, String property) {
        Object ret;
        if (Long.TYPE.isAssignableFrom(clazz)) {
            ret = Long.parseLong(property);
        } else if (Integer.TYPE.isAssignableFrom(clazz)) {
            ret = Integer.parseInt(property);
        } else if (Short.TYPE.isAssignableFrom(clazz)) {
            ret = Short.parseShort(property);
        } else if (Byte.TYPE.isAssignableFrom(clazz)) {
            ret = Byte.parseByte(property);
        } else if (Float.TYPE.isAssignableFrom(clazz)) {
            ret = Float.parseFloat(property);
        } else if (Double.TYPE.isAssignableFrom(clazz)) {
            ret = Double.parseDouble(property);
        } else if (Boolean.TYPE.isAssignableFrom(clazz)) {
            ret = Boolean.parseBoolean(property);
        } else if (Long.class.isAssignableFrom(clazz)) {
            ret = Long.valueOf(property);
        } else if (Integer.class.isAssignableFrom(clazz)) {
            ret = Integer.valueOf(property);
        } else if (Short.class.isAssignableFrom(clazz)) {
            ret = Short.valueOf(property);
        } else if (Byte.class.isAssignableFrom(clazz)) {
            ret = Byte.valueOf(property);
        } else if (Float.class.isAssignableFrom(clazz)) {
            ret = Float.valueOf(property);
        } else if (Double.class.isAssignableFrom(clazz)) {
            ret = Double.valueOf(property);
        } else if (Boolean.class.isAssignableFrom(clazz)) {
            ret = Boolean.valueOf(property);
        } else if (String.class.isAssignableFrom(clazz)) {
            ret = property;
        } else {
            throw new UnsupportedOperationException("unsupport type");
        }

        return ret;
    }
}
